package com.fingard.dsp.bank.directbank.whccb01;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;

/**
 * Created by dalt on 2017/7/18.
 */
public class WHCCB01Transaction extends WHCCB01Base implements ITransaction {
    //采用流处理方式
    //1、循环调用readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用writeRetDetail()，写入返回ATS的明细
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        String opname = "queryAccHisDetail";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("accountNo", p_req7516.actNum);
            params.put("beginDate", Format.Date8Format.format(p_req7516.getDateFrom()));
            params.put("endDate", Format.Date8Format.format(p_req7516.getDateTo()));
            String[] tmpStrRet = sendToBank(params, p_req7516.reqHeader, opname);
            XmlTextReader tmpBkReader;
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = getRespCode(tmpBkReader);
                    if (success(retCode)) {
                        if (tmpBkReader.getFirstText("/BankData/opRep/turnPageTotalNum").equals("0"))//返回0表示无记录
                        {
                            tmpRespCode = "-1";
                            tmpRespInfo = "银行返回记录为空！";
                        } else {
                            // #start "提取各返回节点内容"
                            tmpRespCode = "0";
                            tmpRespInfo = "success";
                            tmpRespCode = "0";
                            tmpRespInfo = "success";
                            while (tmpBkReader.read("/BankData/opRep/RepParam/tranDetail/row")) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                XElement tmpRetNode = tmpBkReader.readedXElement;

                                String dealDate = tmpRetNode.getSingleText("transferDate");
                                String dealTime = tmpRetNode.getSingleText("transferTime");
                                tmpRetDetail.tranDate = DateHelper.getFormat12Date(dealDate);//格式yyyy-MM-dd
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;//格式yyyy-MM-dd
                                tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + DateHelper.getFormat8Time(dealTime);//格式"yyyy-MM-dd HH:mm:ss"
                                tmpRetDetail.comment = tmpRetNode.getSingleTextNotNull("rem").trim();
                                tmpRetDetail.usage = tmpRetNode.getSingleTextNotNull("rem").trim();
                                //tmpRetDetail.abstr = pickupAbstr(tmpRetNode.getSingleTextNotNull("summary"), tmpRetDetail.usage, tmpRetDetail.comment);
                                tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("account").trim();
                                tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("accountName").trim();
                                tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("vouchersNo").trim();
                                tmpRetDetail.askNumber = tmpRetNode.getSingleTextNotNull("transferFlowNo").trim();
                                tmpRetDetail.cur = convertCurToATS(tmpRetNode.getSingleText("currencyType"));
                                String tmpAmount = tmpRetNode.getSingleText("payAmount");
                                if (!tmpAmount.replace("0", "").replace(".", "").equals("")) {
                                    tmpRetDetail.tranAmt = tmpAmount.replace("A", "").replace("a", "").replaceFirst("^0*", "");
                                    if (tmpAmount.startsWith("A") || tmpAmount.startsWith("a")) {
                                        tmpRetDetail.cdFlag = "2";
                                    } else {
                                        tmpRetDetail.cdFlag = "1";
                                    }
                                } else {
                                    tmpAmount = tmpRetNode.getSingleText("recAmount");
                                    tmpRetDetail.tranAmt = tmpAmount.replace("A", "").replace("a", "").replaceFirst("^0*", "");
                                    if (tmpAmount.startsWith("A") || tmpAmount.startsWith("a")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                }

                                tmpRetDetail.accBal = tmpRetNode.getSingleText("balance").replaceFirst("^0*", "");

                                if (tmpRetDetail.accBal.startsWith(".")) {
                                    tmpRetDetail.accBal = "0" + tmpRetDetail.accBal;
                                }

                                if (tmpRetDetail.tranAmt.startsWith(".")) {
                                    tmpRetDetail.tranAmt = "0" + tmpRetDetail.tranAmt;
                                }
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }
                    } else {
                        //  "查询失败"
                        tmpRespCode = "-1";
                        tmpRespInfo = getErrorMsg(tmpBkReader);
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;//设置开始时间和结束时间都为当天
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
